//
//  TPAppointmentViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/27.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPAppointmentViewController.h"
#import "TPAppointDetailViewController.h"
#import "TPProductDetailViewController.h"
#import "TPHomeModel.h"
#import "TPAppointModel.h"
@interface TPAppointmentViewController ()

@end

@implementation TPAppointmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav = self.navigationController;
    
//    self.tableView.backgroundColor = TP_COLOR_245;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(0.0);
//    }];
    [self.tableView layoutIfNeeded];
    @weakify(self);
    [self getRequest];
    [self.tableView xw_addPullToRefreshWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page = 1;
        [self getRequest];
        
    }];
    
    
    [self.tableView xw_addInfinityScrollWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page ++;
        [self getRequest];
    }];
}

-(void)getRequest{
    
    [self.view tp_hideNotMsg];
    [[TPRequestClient sharedClient]getUserAppointment_current:self.page size:kMaxRequestSize success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        if(self.page == 1)
            [self.dataSource removeAllObjects];
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPAppointModel class] json:respone.data[@"data"]];
        [self.dataSource addObjectsFromArray:array];
        [self refreshSccuess];
        
        if(self.dataSource.count == 0){
            [self.view tp_addNotMsg:@"还没有数据~~~"];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        [self refreshFail];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [self refreshFail];
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TPAppointmentCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellx"];
    if(!cell){
        cell = [[TPAppointmentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellx"];
        cell.backgroundColor = [UIColor whiteColor];
    }
    cell.type = self.type;
    cell.model = self.dataSource[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if(self.nav){
        
        if(self.type == TPAppointmentTypeBooking){
             TPAppointModel *model = self.dataSource[indexPath.section];
            TPProductDetailViewController *detailVC = [TPProductDetailViewController new];
            detailVC.model = model.product;
            [self.nav pushViewController:detailVC animated:YES];
            return;
        }
        
        TPAppointDetailViewController *detail = [TPAppointDetailViewController new];
        detail.model = self.dataSource[indexPath.row];
        [self.nav pushViewController:detail animated:YES];
    }
}

@end
